<%-- 
    Document   : editProduct
    Created on : 02 3, 12, 4:20:27 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="Controller.UserManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Controller.ProductManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //if user is not logged in or is not a product manager
        if(user != null && UserManager.isProductManager(user) == 0) {
            response.sendRedirect("../index.jsp");
        }
        //if the user is logged and is a product manager in check for session integrity
        else if( (String)session.getAttribute("user") != null && UserManager.isProductManager(user) != 0 ){
        
            //check session integrity by comparing the hash of the session token
            String currSession = request.getRemoteAddr().concat( session.getId() );
            try{
                if( !UserManager.checkSessionIntegrity(currSession, session) ){
                    UserManager.logout(request);
                    response.sendRedirect("../login.jsp");
                }
            }catch(NoSuchAlgorithmException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(UnsupportedEncodingException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(Exception e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="../css/960_24_col.css" />
        <link rel="stylesheet" href="../css/reset.css" />
        <link rel="stylesheet" href="../css/text.css" />
        <link rel="stylesheet" href="../css/style.css" />
        
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery-ui-1.8.18.custom.min.js"></script>
        <script type="text/javascript" src="../scripts/modalScripts.js"></script>
        <script type="text/javascript" src="../scripts/formScripts.js"></script>
        
        <title>FooBar Online Bookstore | Edit Product</title>
    </head>
    <body>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="../index.jsp"><img src="../images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="../index.jsp">Home</a></li>
                            <li><a href="../main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="../main.jsp?show=book">Books</a></li>
                                    <li><a href="../main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="../main.jsp?show=cd">CD</a></li>
                                    <li><a href="../main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="../main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <li><a href='productPanel.jsp'>Product Panel</a></li>
                            <li><a href="../about.jsp">About</a></li>
                            <li><a href="../contact.jsp">Contact Us</a></li>
                            <%
                            out.println("<li><form action='../logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            %>
                        </ul>
                    </div>
                    <div id="dashboard-user">
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <h1>Edit Product</h1>
                <div id="edit-product-left" class="grid_12">
                    <%
                    try{
                    
                    if( !request.getParameter("id").equals("") ){
                    //get certain product based on index
                    //then print out the results in a form
                    ResultSet product = ProductManager.getProduct( request.getParameter("id") );

                    if( product.next() ){
                    
                    out.println( "<form action='../EditProduct' method='post' id='edit-product-form' class='styled-form'>");
                    %>
                            <label>Product Name: </label><br />
                            <input type="text" name="name" id="edit-product-name" value="<% out.println(product.getString(2));  %>" class="form-inputs" /><br />
                    <%        
                        out.println("<label>Synopsis: </label><br />"
                        + "<textarea name='synopsis' id='edit-product-synopsis' class='form-inputs' cols='40' rows='3'>" + product.getString(3) + "</textarea><br />"
                        + "<label>Genre: </label><br />"
                        + "<select name='genre' id='edit-product-genre'>");
                    
                        ResultSet genres = ProductManager.getProductGenres();

                        while( genres.next() ){
                            out.println("<option value='" + genres.getString(1) + "'");

                            if( genres.getString(1).equals( product.getString(4) ) ){
                                out.println("SELECTED");
                            }

                            out.println(">" + genres.getString(1) + "</option>");
                        }

                        out.println("</select><br />"
                                + "<label>Type: </label><br />"
                                + "<select name='type' id='edit-product-type'>");

                        ResultSet types = ProductManager.getProductTypes();

                        while( types.next() ){
                            out.println("<option value='" + types.getString(1) + "'");

                            if( types.getString(1).equals( product.getString(5) ) ){
                                out.println("SELECTED");
                            }

                            out.println(">" + types.getString(1) + "</option>");
                        }

                        out.println("</select><br />"
                                + "<label>Price: </label><br />"
                                + "<input type='text' name='price' id='edit-product-price' value='" + product.getFloat(6) + "' class='form-inputs' /><br />"
                                + "<label>Available</label>"
                                + "<input type='checkbox' name='status' value='1' ");

                        //@TODO problem when the checkbox is not checked
                        if( product.getInt(7) == 1 ){
                            out.println("CHECKED");
                        }

                        out.println("/><br />"
                                + "<input type='hidden' name='product' value='" + product.getString(8) + "' />"
                                + "<input type='submit' value='Save Changes' class='submit-button' />"
                                + "<input type='reset' value='Reset' class='submit-button' />"
                                + "</form>");
                    }
                    else{
                        out.println("<h1>No Product To Dsiplay</h1>");
                    }
                    %>
                </div>
                <div class="grid_12" id="edit-product-right">
                    <ul class="error-pane">
                        <%
                        try{
                            if( request.getParameter("status").equals("1") ){
                                out.println("<li class='important success'>Saved Changes</li>");
                            }
                            else if( request.getParameter("status").equals("0") ){
                                out.println("<li class='important'>Fill Up The Form Correctly</li>");
                            }
                            else if( request.getParameter("status").equals("-1") ){
                                out.println("<li class='important'>There is Already A Product With The Same Name and Type</li>");
                            }
                            else if( request.getParameter("status").equals("-2") ){
                                out.println("<li class='important'>Oooops! Something Went Wrong Please Try Again Later</li>");
                            }
                        }
                        catch(NullPointerException e1){}
                        catch(Exception e1){}
                        %>
                        <li style="display: none;" id='edit-product-name-error'>please enter a valid product name (alphanumeric characters only)</li>
                        <li style="display: none;" id='edit-product-synopsis-error'>the synopsis is too long</li>
                        <li style="display: none;" id='edit-product-synopsis-none'>please enter a synopsis</li>
                        <li style="display: none;" id='edit-product-genre-error'>please select a genre for your product</li>
                        <li style="display: none;" id='edit-product-type-error'>please select the type of your product</li>
                        <li style="display: none;" id='edit-product-price-error'>please enter a valid price</li>
                        <%
                        }
                        else{
                            out.println("<div class='grid_12'>"
                                + "<h1>There is no product to view</h1>"
                                + "<a href='main.jsp'>Back</a>"
                                + "</div>");
                        }
                    
                        }
                        catch(NullPointerException e1){
                            out.println("<div class='grid_12'>"
                                + "<h1>There is no product to view</h1>"
                                + "<a href='main.jsp'>Back</a>"
                                + "</div>");
                        }
                        %>
                    </ul>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
